<template>
  <el-dialog title="编辑年段信息" :visible.sync="dialogVisible" :close-on-click-modal="false"
             :close-on-press-escape="false" :before-close="handleClose" @close="$emit('update:visible',false)"
             top="100px">
    <el-form :model="editForm" label-width="160px" :rules="editFormRules" ref="editForm">
      <el-form-item label="年段名称：" prop="name">
        <el-input v-model="editForm.section_name" auto-complete="off" placeholder="填写年段名称"></el-input>
      </el-form-item>
      <el-form-item label="是否升学/毕业：" prop="name">
        <el-radio-group v-model="editForm.type" @change="radiochange">
          <el-radio :label="0">无</el-radio>
          <el-radio :label="1">升学</el-radio>
          <el-radio :label="2">毕业</el-radio>
        </el-radio-group>
        <p>选择创建的年段学生是否需要升学或者毕业</p>
      </el-form-item>
      <el-form-item label="升学年段：" prop="name" v-if="editForm.type==1">
        <el-select v-model="editForm.upgrade_id" placeholder="请选择升学年段" @change="sectionChange" class="mr-10">
          <el-option :label="item.section_name" :value="item.id" v-for="(item, index) in arryList" :key="index"></el-option>
        </el-select>
        <p>如未找到相应年段请先添加年段</p>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click.native="resetForm" size="small">取消</el-button>
      <el-button type="primary" @click.native="editSubmit" size="small">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import api from '@/utils/api.js'

  export default {
    name: 'Dialog',
    props: ['visible', 'editId'],
    watch: {
      visible () {
        this.dialogVisible = this.visible
        if (this.visible && this.editId) {
          console.log('编辑')
          this.editForm.section_name = this.editId.section_name
          this.editForm.type = this.editId.type
          this.editForm.upgrade_id = this.editId.upgrade_id
        }
        if (this.visible && !this.editId) {
          this.editForm.section_name = ''
          this.editForm.type = 0
          this.editForm.upgrade_id = 0
        }
        if (this.visible) {
          this.InitFn()
        }
      }
    },

    data () {
      return {
        tieleName: '',
        dialogVisible: false,
        editForm: {
          section_name: '',
          type: 0,
          upgrade_id: 0
        }, // 编辑界面数据
        editFormRules: {
          section_name: [
            {required: true, message: '请填写年段', trigger: 'blur'}
          ]
        }, // 表单验证不能为空规则
        section_id: '',
        arryList: []
      }
    },
    created () {

    },
    methods: {
      InitFn () {
        this.$post(api.admin_section_index, {
          p: 1
        }).then((response) => {
          if (response.code === 0) {
            this.arryList = response.list.data
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      handleClose () {
        this.dialogVisible = false
      },
      resetForm () {
        this.dialogVisible = false
      },
      editSubmit () {
        let url_ = api.admin_section_add
        if (this.editId) {
          url_ = api.admin_section_edit
          this.editForm.id = this.editId.id
        }
        this.$post(url_, this.editForm).then((response) => {
          if (response.code === 0) {
            this.$emit('refreshFn')
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.dialogVisible = false
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      radiochange () {
        console.log('type:', this.editForm.type)
        if (this.editForm.type !== 1) {
          this.editForm.upgrade_id = 0
        }
      },
      sectionChange () {

      }
    }
  }
</script>
